<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <c:import url="/static/common/easyui_header.jsp"/>
    <title>考试管理界面</title>

    <script type="text/javascript">
        $(function () {

            var testDatagrid = $("#test_datagrid");

            testDatagrid.datagrid({
                url:'/test/list',
                fitColumns:true,
                rownumbers:true,
                singleSelect:true,
                pagination:true,
                fit:true,
                toolbar:'#test_toolbar',
                columns:[[
                    {field:'id',title:'考试编号',width:100},
                    {field:'client.name',title:'考生姓名',formatter:formatterClientName,width:100, align: 'center'},
                    {field:'client.saleman.name',title:'营销人',formatter:formatterSaleManName,width:100, align: 'center'},
                    {field:'client.qq',title:'QQ',formatter:formatterQQ,width:100, align: 'center'},
                    {field:'client.tel',title:'电话',formatter:formatterTel,width:100, align: 'center'},
                    {field:'client.clz',title:'意向班级',formatter:formatterClz,width:100, align: 'center'},
                    {field:'testDate',title:'考试时间',width:100, align: 'center'},
                    {field:'testResult',title:'考试结果',formatter:formatterTestResult,width:100, align: 'center'},
                    {field:'handler',title:'批阅人',formatter:formatterHandler,width:100, align: 'center'}
                ]]
            });

            var cmdObj = {
                //刷新
                'reload':function () {
                    testDatagrid.datagrid("reload");
                },
                //通过
                'changStateSuccess':function () {
                    var selectedRow = testDatagrid.datagrid("getSelected");
                    if (!selectedRow) {
                        $.messager.alert("提示","请选择要通过的对象","error");
                        return;
                    }
                    if (selectedRow.testResult){
                        $.messager.alert("提示","该对象已经被操作,请重新选择","error");
                        return;
                    }
                    $.messager.confirm("提示","确认要通过吗?",function (yes) {
                        if (yes) {
                            $.post("/test/audit",{'testResult':1,'id':selectedRow.id},function (data) {
                                if (data.success) {
                                    $.messager.alert("提示","通过成功","info",function () {
                                        cmdObj.reload();
                                    });
                                }
                            },'json');
                        }
                    })
                },
                //不及格
                'changStateFail':function(){
                    var selectedRow = testDatagrid.datagrid("getSelected");
                    if (!selectedRow) {
                        $.messager.alert("提示","请选择不及格的对象","error");
                        return;
                    }
                    if (selectedRow.testResult){
                        $.messager.alert("提示","该对象已经被操作,请重新选择","error");
                        return;
                    }
                    $.messager.confirm("提示","真的要不及格吗?",function (yes) {
                        if (yes) {
                            $.post("/test/audit",{'testResult':-1,'id':selectedRow.id},function (data) {
                                if (data.success) {
                                    $.messager.alert("提示","设置为不及格成功","info",function () {
                                        cmdObj.reload();
                                    });
                                }
                            },'json');
                        }
                    })
                },
                //根据考生名字查询
                'searchForEmp':function () {
                    testDatagrid.datagrid("load",{'clientName':$("[name='clientName']").val()});
                },
                //只看未批阅
                'onlySee':function () {
                    testDatagrid.datagrid("load",{'testResult':0});
                },
                //查看所有
                'seeAll':function () {
                    testDatagrid.datagrid("load",{});
                },
                //删除
                'delete':function () {
                    var selectedRow = testDatagrid.datagrid("getSelected");
                    if (!selectedRow) {
                        $.messager.alert("提示","请选择一行想要删除的数据","error");
                        return;
                    }
                    $.messager.confirm("提示","真的要删除吗?",function (yes) {
                        if (yes) {
                            $.post("/test/delete",{'testId':selectedRow.id},function (data) {
                                if (data.success) {
                                    $.messager.alert("提示","删除成功","info",function () {
                                        cmdObj.reload();
                                    });
                                } else {
                                    $.messager.alert("提示",data.msg,"error");
                                }
                            },'json');
                        }
                    })
                }

            };


            //统一给data-cmd绑定点击事件
            $("a[data-cmd]").click(function () {
                var cmd = $(this).data('cmd');
                cmdObj[cmd]();
            });


        })
        
        function formatterClientName(value,row,index) {
            if (row.client.name) {
                return row.client.name;
            }
        }
        function formatterSaleManName(value,row,index) {
            if (row.client.saleman) {
                return row.client.saleman.realName;
            }
        }
        function formatterQQ(value,row,index) {
            if (row.client.qq) {
                return row.client.qq;
            }
        }
        function formatterTel(value,row,index) {
            if (row.client.tel) {
                return row.client.tel;
            }
        }
        function formatterClz(value,row,index) {
            if (row.client.clz) {
                return row.client.clz.name;
            }
        }
        function formatterTestResult(value,row,index) {
                if (!value) {
                    return '<span style="color: #9d9d9d">待批阅</span>';
                } else if (value == 1){
                    return '<span style="color: green">通过</span>';
                } else {
                    return '<span style="color: red">未通过</span>';
                }
        }
        function formatterHandler(value,row,index) {
            if (row.handler){
                return row.handler.realName;
            }
        }
    </script>
</head>
<body>
    <table id="test_datagrid"></table>

    <div id="test_toolbar">
        <a id="btn_changeStateSuccess" class="easyui-linkbutton" iconCls="icon-ok" plain="true" data-cmd="changStateSuccess">通过</a>
        &emsp;
        <a id="btn_changeStateFail" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" data-cmd="changStateFail">不及格</a>
        &emsp;
        <a class="easyui-linkbutton" iconCls="icon-remove" plain="true" data-cmd="delete">删除</a>
        &emsp;
        <a class="easyui-linkbutton" iconCls="icon-reload" plain="true" data-cmd="reload">刷新</a>
        &emsp;&emsp;
        <span style="color: darkblue">潜在学员姓名:</span><input class="easyui-textbox" name="clientName">
        <a class="easyui-linkbutton" iconCls="icon-search" plain="true" data-cmd="searchForEmp">查询</a>
        &emsp;&emsp;
        <a class="easyui-linkbutton" iconCls="icon-filter" plain="true" data-cmd="onlySee">只看未批阅</a>
        &emsp;&emsp;
        <a class="easyui-linkbutton" iconCls="icon-see" plain="true" data-cmd="seeAll">查看所有</a>
    </div>
</body>
</html>
